﻿using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Web;

namespace app.scrum
{
    public abstract class ScrumPage : System.Web.UI.Page
    {
        protected ScrumObject sObject;

        public ScrumPage()
        {
            this.sObject = new MySqlScrumObject();            
        }

        public virtual void read(HttpContext context)
        {
            try
            {
                HttpResponse resp = null;
                if (context != null)
                {
                    resp = context.Response;
                }
                else
                {
                    resp = this.Response;
                }
                if (Util.isEmpty(this.sObject.readCmdText))
                {
                    this.sObject.genReadCmdText();
                }                
                ArrayList sObjList = this.sObject.read();
                string json = JsonHelper.getJson(sObjList);
                resp.Write(json);
            }
            catch (Exception ex)
            {
                this.handleException(context.Response, ex);
            }
        }

        public virtual void create(HttpContext context, ArrayList sObjList)
        {
            int num = 0;
            long maxId = 0;
            foreach (ScrumObject sObj in sObjList)
            {
                if (Util.isEmpty(this.sObject.createCmdText))
                {
                    if (maxId == 0)
                    {
                        maxId = this.sObject.getMaxId();
                    }
                    maxId++;
                    ScrumField id = new ScrumField();
                    id.name = ScrumField.fieldName_Id;
                    id.value = maxId;
                    sObj.addField(sObj, id);
                    this.sObject.genCreateCmdText(sObj);
                }
                num += this.sObject.create(sObj);
            }
            context.Response.Write(num);
        }

        public virtual void update(HttpContext context, ArrayList sObjList)
        {
            int num = 0;
            foreach (ScrumObject sObj in sObjList)
            {
                if (Util.isEmpty(this.sObject.updateCmdText))
                {
                    this.sObject.genUpdateCmdText(sObj);
                }
                num += this.sObject.update(sObj);
            }
            context.Response.Write(num);
        }

        public void delete(HttpContext context, ArrayList sObjList)
        {
            int num = 0;
            foreach (ScrumObject sObj in sObjList)
            {
                if (Util.isEmpty(this.sObject.deleteCmdText))
                {
                    this.sObject.genDeleteCmdText(sObj);
                }
                num += this.sObject.delete(sObj);
            }
            context.Response.Write(num);
        }

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            //this.read(null);
        }

        public virtual void ProcessRequest(HttpContext context)
        {
            if (context.Request != null)
            {
                HttpRequest req = context.Request;
                HttpResponse resp = context.Response;
                string action = req.Params.Get("action");
                string root = req.Params.Get("root");
                ArrayList sObjList = JsonHelper.getScrumObjectList(root);
                if (Util.isEmpty(action) || String.Equals(action, "read"))
                {
                    this.read(context);
                }
                else if (String.Equals(action, "create"))
                {
                    ArrayList createList = new ArrayList();
                    ArrayList updateList = new ArrayList();
                    foreach (ScrumObject sObj in sObjList)
                    {
                        if (ScrumObjectHelper.getScrumField(sObj, ScrumField.fieldName_Id) == null)
                        {
                            createList.Add(sObj);
                        }
                        else
                        {
                            updateList.Add(sObj);
                        }
                    }
                    this.create(context, createList);
                    this.update(context, updateList);
                }
                else if (String.Equals(action, "destroy"))
                {
                    this.delete(context, sObjList);
                }
            }
            base.ProcessRequest(context);
        }

        public virtual void handleException(HttpResponse resp, Exception ex)
        {
            resp.Write(ex);
        }
    }
}